package com.njts.servlet;

import com.njts.bean.*;
import com.njts.bean.Class;
import com.njts.dao.UseDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/getHtml")
public class GetJSPServlet extends HttpServlet {
    UseDao useDao=new UseDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(200);
        String name=req.getParameter("name");
        if(name.equals("学生")) {
            ArrayList<Student> studentList= (ArrayList<Student>) useDao.selectStudent();
            ArrayList<Class> classList=(ArrayList<Class>) useDao.selectClass();
            req.setAttribute("classList",classList);
            req.setAttribute("studentList",studentList);
            req.setAttribute("studentCount",20); //先写死后面改
            req.getRequestDispatcher("/view/student.jsp").forward(req, resp);
        }else if(name.equals("课程")){
            ArrayList<Course> courseList=(ArrayList<Course>) useDao.selectCourse();
            ArrayList<Class> classList=(ArrayList<Class>) useDao.selectClass();
            ArrayList<Subject> subjectList=(ArrayList<Subject>) useDao.selectSubject();
            ArrayList<Teacher> teacherList=(ArrayList<Teacher>) useDao.selectTeacher();
            req.setAttribute("classList",classList);
            req.setAttribute("subjectList",subjectList);
            req.setAttribute("teacherList",teacherList);
            req.setAttribute("courseList",courseList);
            req.setAttribute("courseCount",20); //先写死后面改
            req.getRequestDispatcher("/view/course.jsp").forward(req,resp);
        }else if(name.equals("老师")){
            ArrayList<Teacher> teacherList =(ArrayList<Teacher>) useDao.selectTeacher();
            ArrayList<Class> classList=(ArrayList<Class>) useDao.selectClass();
            ArrayList<Subject> subjectList=(ArrayList<Subject>) useDao.selectSubject();
            req.setAttribute("subjectList",subjectList);
            req.setAttribute("classList",classList);
            req.setAttribute("teacherList",teacherList);
            req.setAttribute("teacherCount",20); //先写死后面改
            req.getRequestDispatcher("/view/teacher.jsp").forward(req,resp);
        }else if(name.equals("班级")){
            ArrayList<Class> classList=(ArrayList<Class>) useDao.selectClass();
            req.setAttribute("classList",classList);
            req.setAttribute("classCount",20); //先写死后面改
            req.getRequestDispatcher("/view/class.jsp").forward(req,resp);
        }else if(name.equals("成绩")){
            ArrayList<Score> scoreList=(ArrayList<Score>) useDao.selectScore();
            ArrayList<Subject> subjectList=(ArrayList<Subject>) useDao.selectSubject();
            req.setAttribute("subjectList",subjectList);
            req.setAttribute("scoreList",scoreList);
            req.setAttribute("scoreCount",20); //先写死后面改
            req.getRequestDispatcher("/view/score.jsp").forward(req,resp);
        }else if(name.equals("用户")){
            ArrayList<User> userList=(ArrayList<User>) useDao.selectUser();
            req.setAttribute("userList",userList);
            req.setAttribute("userCount",20);
            req.getRequestDispatcher("/view/user.jsp").forward(req,resp);
        }else if(name.equals("科目")){
            ArrayList<Subject> subjectList=(ArrayList<Subject>) useDao.selectSubject();
            req.setAttribute("subjectList",subjectList);
            req.setAttribute("subjectCount",20); //先写死后面改
            req.getRequestDispatcher("/view/subject.jsp").forward(req,resp);
        }
    }
}
